<?php

namespace app\webapi\vms\service\cms;

use app\webapi\common\BaseService;
use app\webapi\vms\model\ChannelModel;
use app\webapi\vms\model\CompanyModel;
use app\webapi\vms\model\HonorModel;
use library\OWException;

/**
 * @单页服务类
 */
class SingleService  extends BaseService
{
    /**
     * @desc 设置发展历程信息
     * @param $params
     * @return true
     * @throws \Exception
     * @author zhaozhiwei
     * @time 2023/12/14-10:51
     */
    public function setHonoraryQualifications($params,$website_id){
        ChannelModel::where(['id'=>$params['channel_id']])->save(['name'=>$params['name'],'subtitle'=>$params['subtitle']]);
        $json_array=json_decode($params['content'],true);
        foreach($json_array as &$v){
            $v['website_id']=$website_id;
        }
        unset($v);
        $model=new HonorModel();
        $model->where(['website_id'=>$website_id])->update(['delete_time'=>time()]);
        $model->saveAll($json_array);
        return true;
    }

    /**
     * @desc 获取发展历程信息
     * @param $website_id
     * @param $channel_id
     * @return ChannelModel|array|mixed|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaozhiwei
     * @time 2023/12/14-10:51
     */
    public function getHonoraryQualifications($channel_id,$website_id)
    {
        $channel_info=ChannelModel::field(['id','name','subtitle'])->where(['id'=>$channel_id])->find();
        if(empty($channel_info)){
            throw new OWException('栏目不存在');
        }

        $where=['website_id'=>$website_id,'delete_time'=>0];
        $fields=['id','name','img','of_date','desc','create_time'];
        $channel_info['content']=HonorModel::field($fields)->where($where)->select()->toArray()??[];
        return $channel_info;
    }

    /**
     * @desc 设置联系我们信息
     * @param $params
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaozhiwei
     * @time 2023/12/14-11:13
     */
    public function setContactUs($params,$website_id){
        ChannelModel::where(['id'=>$params['channel_id']])->save(['name'=>$params['name'],'subtitle'=>$params['subtitle']]);
        unset($params['channel_id'],$params['name'],$params['subtitle']);

        $model=new CompanyModel();
        $company_info=$model->where(['website_id'=>$website_id])->find();
        if(empty($company_info)){
            $model->save($params);
        }else{
            $model->where(['website_id'=>$website_id])->save($params);
        }
        return true;
    }

    /**
     * @desc 获取联系我们信息
     * @param $website_id
     * @param $channel_id
     * @return ChannelModel|array|mixed|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaozhiwei
     * @time 2023/12/14-11:14
     */
    public function getContactUs($channel_id,$website_id)
    {
        $channel_info=ChannelModel::field(['id','name','subtitle'])->where(['id'=>$channel_id])->find();
        $channel_info['content']=CompanyModel::where(['website_id'=>$website_id])->select()->toArray()??[];
        return $channel_info;
    }
}